package yz.cp.back.s.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yz.cp.back.common.constant.CommonConstant;
import yz.cp.back.s.entity.SysRole;
import yz.cp.back.s.entity.SysUser;
import yz.cp.back.s.entity.SysUserRole;
import yz.cp.back.s.mapper.SysUserRoleMapper;
import yz.cp.back.s.service.ISysUserRoleService;
import yz.cp.back.s.service.ISysUserService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-04-03
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    @Autowired
    private ISysUserService userService;

    @Override
    public List<SysRole> findByUserId(String userId) {

        return this.baseMapper.findByUserId(userId);
    }

    @Override
    public List<String> findDepIdsByUserId(String userId) {

        return this.baseMapper.findDepIdsByUserId(userId);
    }

    @Override
    public List<SysUserRole> findByRoleId(String roleId) {
        return list(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getRoleId, roleId));
    }

    @Override
    public List<SysUser> findUserByRoleId(String roleId) {

        List<SysUserRole> userRoleList = findByRoleId(roleId);
        List<SysUser> list = new ArrayList<>();
        for (SysUserRole ur : userRoleList) {
            SysUser u = userService.getById(ur.getUserId());
            if (u != null && CommonConstant.USER_STATUS_NORMAL.equals(u.getStatus())) {
                list.add(u);
            }
        }
        return list;
    }

    @Override
    public void deleteByUserId(String userId) {
        remove(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, userId));
    }
}